package com.kxj.xfyy;

import android.R.integer;
import android.content.Context;
import android.util.Log;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;

public class TtsSpeechSynthesizer {
	private static String TAG = "TtsPlugin";
	private static final String APP_ID="59055fdf";
	private static SpeechSynthesizer speechSynthesizer;
	
	/**
	 * 初始化监听码
	 */
	public static int initCode=0;
	private TtsSpeechSynthesizer() {

	}

	public static SpeechSynthesizer getSpeechSynthesizer(Context context) {
		SpeechUtility
				.createUtility(context, SpeechConstant.APPID + "="+APP_ID);
		if (speechSynthesizer == null) {
			speechSynthesizer = SpeechSynthesizer.createSynthesizer(context,
					new InitListener() {
						@Override
						public void onInit(int code) {
							initCode=code;
							if (initCode != ErrorCode.SUCCESS) {
								Log.i(TAG, "初始化失败,错误码:" + initCode + ",原因:"
										+ ErrorInfo.getError(initCode));
							} else {
								Log.i(TAG, "初始化成功");
							}
						}
					});
		}
		return speechSynthesizer;
	}
	public static void setParameter(){
		if(speechSynthesizer!=null){
			speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); //设置发音人
			speechSynthesizer.setParameter(SpeechConstant.SPEED, "50");//设置语速
			speechSynthesizer.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围 0~100
			speechSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
			//设置合成音频保存位置（可自定义保存位置） ，保存在“./sdcard/iflytek.pcm”
			//保存在 SD 卡需要在 AndroidManifest.xml 添加写 SD 卡权限
			//仅支持保存为 pcm 和 wav 格式，如果不需要保存合成音频，注释该行代码
			//speechSynthesizer.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");
		}
	}
}
